// pages/user/index.js
const app = getApp();
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickName: '',
    avatarUrl: "",
    defaultAvatarUrl: defaultAvatarUrl,
    admin: 0,
    promise: false,
    subAdmin: 0,
    showGetPhone: false,
    addRouter: true,
    tel: '',
    showUser: false,
    affiliations: [],
    // inDept: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.hideShareMenu();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init();
  },

  init(){
    this.getAffiliations();
    this.getDepartmentList();
    this.getAdmin();
    this.setData({
      nickName: wx.getStorageSync('nickName') || '',
      avatarUrl: wx.getStorageSync('headPortrait') || defaultAvatarUrl,
      tel: wx.getStorageSync('userPhone') || ''
    });
  },

  getAffiliations() {
    wx.request({
      url: `${app.globalData.baseUrl}/api/finance/getDictDatas`,
      method: "GET",
      header: {
        'token': wx.getStorageSync('token'),
        "Content-Type": "application/json"
      },
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({ affiliations: res.data.data });
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  logout() {
    wx.showModal({
      title: '提示',
      content: '您确定要退出登录吗？',
      success(res) {
        if (res.confirm) {
          wx.removeStorageSync('userPhone');
          setTimeout(() => {
            // app.onLogin();
            wx.reLaunch({
              url: '/pages/index/index'
            });
          }, 1000)
        }
      }
    });
  },

  shareCode() {
    wx.navigateTo({
      url: '/pages/shareCode/index',
    });
  },

  setAdmin() {
    wx.navigateTo({
      url: '/pages/setAdmin/index',
    });
  },

  linkCaiwu() {
    wx.navigateTo({
      url: '/pages/statisticians/index',
    });
  },

  linkMenchuang() {
    wx.navigateToMiniProgram({
      appId: 'wx987c9d744798636f',
      path: '/pages/index/index',
      envVersion: 'release',
      success(res) {
        console.log(res)
      }
    });
  },

  linkClassify() {
    wx.navigateTo({
      url: '/pages/classify/index',
    });
  },

  linkTypeManage() {
    wx.navigateTo({
      url: '/pages/typeManage/index',
    });
  },

  linkSales() {
    wx.navigateTo({
      url: '/pages/sales/index',
    });
  },

  setManage() {
    wx.navigateTo({
      url: '/pages/webView/index',
    });
  },

  privacyPolicy() {
    wx.navigateTo({
      url: '/pages/privacyPolicy/index',
    });
  },

  login() {
    this.setData({ showGetPhone: true });
  },

  getAdmin() {
    wx.request({
      url: `${app.globalData.baseUrl}/api/accounting/selManage`,
      method: "GET",
      header: {
        'token': wx.getStorageSync('token'),
        "Content-Type": "application/json"
      },
      success: (res) => {
        if (res.data.code == 0) {
          const arr = [];
          const subArr = [];
          res.data.data?.forEach(item => {
            if(item.status === '1') arr.push(item.tel);
            if(item.status === '0') subArr.push(item.tel);
          });
          if (arr.indexOf(wx.getStorageSync('userPhone').toString()) > -1) {
            wx.setStorageSync('admin', 1);
            this.setData({ admin: 1 });
          } else {
            wx.setStorageSync('admin', 0);
            this.setData({ admin: 0 });
          };
          if (subArr.indexOf(wx.getStorageSync('userPhone').toString()) > -1) {
            this.setData({ subAdmin: 1 });
            wx.setStorageSync('subAdmin', 1);
          } else {
            this.setData({ subAdmin: 0 });
            wx.setStorageSync('subAdmin', 0);
          };
        } else {
          wx.showToast({
            title: res.data.msg || '出错啦！',
            icon: 'error',
            duration: 2000
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.init();
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000);
  },

  // 获取部门列表
  getDepartmentList() {
    wx.request({
      url: `${app.globalData.baseUrl}/api/finance/getUserDept`,
      method: "GET",
      header: {
        'token': wx.getStorageSync('token'),
        "Content-Type": "application/json"
      },
      success: (res) => {
        if (res.data.code == 0) {
          const deptIds = [];
          res?.data?.data?.forEach(item => {
            deptIds.push(item.deptId);
          });
          this.setData({
            promise: deptIds.indexOf(1) > -1 && deptIds.indexOf(2) > -1 && deptIds.indexOf(3) > -1
          });
        }
      }
    })
  },

  closePhone(e) {
    this.setData({ showGetPhone: false });
    if (e.detail) this.setData({ showUser: true });
    this.init();
  },

  editUser() {
    this.setData({ showUser: true });
  },

  closeUser() {
    this.setData({
      nickName: wx.getStorageSync('nickName') || '',
      avatarUrl: wx.getStorageSync('headPortrait') || defaultAvatarUrl,
      showUser: false
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})